﻿using RBAC.LuFang.Domain;
using RBAC.LuFang.Domain.DTO;
using RBAC.LuFang.Repository.lmpl;
using RBAC.LuFang.Repository.lnterface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using static System.Collections.Specialized.BitVector32;

namespace RBAC.LuFang.Domin.Services
{
    public class RoleService: IRoleService
    {
        private readonly IRoleRepository _IroleRepository;//角色
        private readonly IUserRepository _IuserRepository;//用户
        private readonly IUserRoleRepository _IuserroleRepository;//用户角色
        //构造函数
        public RoleService(IRoleRepository iroleRepository, IUserRepository iuserRepository, IUserRoleRepository IuserroleRepository)
        {
            _IroleRepository = iroleRepository;
            _IuserRepository = iuserRepository;
            _IuserroleRepository = IuserroleRepository;
        }

        /// <summary>
        /// 添加角色
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        public async Task<int> AddRole(Role role)
        {
            try
            {
                role.addName = "张三";
                role.addDate = DateTime.Now;
                var res = await _IroleRepository.Get(x => x.RoleName == role.RoleName);
                if (res.Count() > 0)
                {
                    return -1;//角色名称存在
                }
                return await _IroleRepository.GetAddAsync(role);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 显示角色
        /// </summary>
        /// <returns></returns>
        public async Task<Page> GetAllRoles(int pageindex, int pagesize,string? rname)
        {
            try
            {
                Page page = new Page();
                var res = (await _IroleRepository.GetAllAsync()).Where(x => x.IsDel == false);
                if (!string.IsNullOrEmpty(rname))
                {
                    res = res.Where(x => x.RoleName.Contains(rname));
                }
                page.SumCount = res.Count();
                page.LastPage = (int)Math.Ceiling((page.SumCount * 1.0) / pagesize);
                page.Roles = res.OrderByDescending(x => x.addDate).Skip((pageindex - 1) * pagesize).Take(pagesize).ToList();
                return page;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DelRole(int id)
        {
            try
            {
                var res=await _IroleRepository.GetByidAsync(id);
                res.IsDel = true;
                return await _IroleRepository.GetUpd(res);
            }
            catch (Exception)
            {

                throw;
            }
        }

        

        /// <summary>
        /// 角色反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<Role> GetByid(int id)
        {
            try
            {
                return await _IroleRepository.GetByidAsync(id);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 角色修改
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        public async Task<int> UpdRole(Role role)
        {
            try
            {
                role.updName = "李四";
                role.updDate = DateTime.Now;
                var res=await _IroleRepository.Get(x=>x.RoleName == role.RoleName);
                if(res.Count() > 0)
                {
                    return -1;//修改的角色名称存在
                }
                return await _IroleRepository.GetUpd(role);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 级联
        /// </summary>
        public class Menutun
        {
            public int value { get; set; }
            public string? label { get; set; }
            public List<Menutun> children { get; set; }
        }
        /// <summary>
        /// 根据部门ID查询员工名称
        /// </summary>
        /// <param name="sectionId"></param>
        /// <returns></returns>
        public async Task<List<GetemployeeDTO>> Getemployee(string? sectionId)
        {
            try
            {
                var res =await _IuserRepository.Get(x=>sectionId.Contains(x.sectionId.ToString()));
                var list = res.Select(x => new Menutun
                {
                    value=x.userInfoID,
                    label=x.userName,
                });
                var dto = list.ToList().Select(x => new GetemployeeDTO
                {
                    value=x.value,
                    label=x.label,
                });
                return dto.ToList();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 添加用户角色
        /// </summary>
        /// <param name="userRole"></param>
        /// <returns></returns
        /// >
        public async Task<int> AddUserRole(int rid,List<int> uid)
        {
            var res1 = 0;
            foreach (var item in uid)
            {
                    var count = await _IuserroleRepository.Get(x => x.RoleId == rid && x.UserRoleId == item && x.IsDel==false);
                    if (count.Count > 0)
                    {
                        res1= - 1;
                    }
                    else
                    {
                        res1 = 1;
                        var res = new UserRole();
                        res.RoleId = rid;
                        res.userInfoID = item;
                        await _IuserroleRepository.GetAddAsync(res);
                    }
            }
            return res1;
        }
        public class BuMenDto
        {
            public int uid { get; set; }
            public int rid { get; set; }
            public string ?Label { get; set; }
        }
        /// <summary>
        /// 反填部门树形
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        public async Task<List<GetRoleDTO>> GetRole(int roleId)
        {
            try
            {
                var res = from a in await _IuserRepository.GetAllAsync()
                          join b in await _IuserroleRepository.GetAllAsync()
                          on a.userInfoID equals b.userInfoID
                          where b.RoleId == roleId && b.IsDel == false
                          select new
                          {
                              b.RoleId,
                              a.userInfoID,
                              a.sectionId
                          };
                var dto = res.ToList().Select(x => new GetRoleDTO
                {
                    RoleId=x.RoleId,
                    userInfoID=x.userInfoID,
                    sectionId=x.sectionId
                });
                return dto.ToList();
            }
            catch (Exception)
            {

                throw;
            }
        }
        
        /// <summary>
        /// 显示某角色的员工
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        public async Task<List<GetRoleNameDTO>> GetRoleName(int roleId)
        {
            try
            {
                var res = (from a in await _IuserRepository.GetAllAsync()
                          join b in await _IuserroleRepository.GetAllAsync()
                          on a.userInfoID equals b.userInfoID
                          join c in await _IroleRepository.GetAllAsync()
                          on b.RoleId equals c.RoleId
                          where b.RoleId == roleId && b.IsDel==false
                          select new GetRoleNameDTO
                          {
                              //a.userInfoID,
                              userName=a.userName,
                              UserRoleId=b.UserRoleId,
                              //c.RoleId
                          }).ToList();
                return res;

            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 删除角色下的员工
        /// </summary>
        /// <param name="urId"></param>
        /// <returns></returns>
        public async Task<int> Del(int urId)
        {
            try
            {
                var res=await _IuserroleRepository.GetByidAsync(urId);
                res.IsDel = true;
                return await _IuserroleRepository.GetUpd(res);

            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
